<template>
  <div class="container">
    <div class="title">
      <h1 style="text-shadow: 10px 5px 5px rgba(66, 64, 255, 0.3)">LIMS</h1>
    </div>
    <!-- 登录表单 -->
    <el-card class="box-card">
      <div slot="header" class="header">
        <span v-if="isLogin">用户登录</span>
        <span v-else>用户注册</span>
        <el-button type="text" class="toggle-button" @click="toggleForm">
          {{ isLogin ? '没有账号？注册' : '已有账号？登录' }}
        </el-button>
      </div>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password"></el-input>
        </el-form-item>
        <el-form-item v-if="!isLogin" label="确认密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin: 0 auto" @click="handleSubmit">{{ isLogin ? '登录' : '注册' }}</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { login } from '../../api/user';
import { useRoute, useRouter } from 'vue-router';

const router = useRouter();
const route = useRoute();

interface Form {
  username: string
  password: string
  confirmPassword?: string
}

const isLogin = ref(true)
const form = ref<Form>({
  username: '',
  password: '',
  confirmPassword: ''
})
const formRef = ref()

const rules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: !isLogin.value, message: '请确认密码', trigger: 'blur' },
    { validator: (rule: any, value: string, callback: any) => {
        if (!isLogin.value && value !== form.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      }, trigger: 'blur'
    }
  ]
})

const toggleForm = () => {
  isLogin.value = !isLogin.value
  form.value = { username: '', password: '', confirmPassword: '' }
}

const handleSubmit = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      if (isLogin.value) {
        // 执行登录操作
        console.log(form.value,'--------------')
        const response = await login(form.value.username,form.value.password);

        if(response.data) {console.log(response,'*******')
          ElMessage.success('登录成功')
          router.push({name: "Home"})
        }else{
          ElMessage.warning("登录失败")
        }
      } else {
        // 执行注册操作
        ElMessage.success('注册成功')
      }
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
</script>

<style scoped>
.title {
  font-size: 36px;
  color: #409eff;
}

.container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
  background-image: url(../../assets/bg.jpg);
  background-size: 100%;
}

.box-card {
  width: 400px;
  background-color: #94a2a3;
  border-color: #94a2a3;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.toggle-button {
  font-size: 14px;
  color: #409eff;
  cursor: pointer;
}
</style>
